﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class terrainGen_FloorMaker : MonoBehaviour {

	//---------------------------------------------------------------------------------------------
	// Properties
	public int movecount;
	public int movemax;
	public int loc_x = (terrainGeneration.terrainHeight/2);
	public int loc_y = (terrainGeneration.terrainHeight/2);
	
	
	//---------------------------------------------------------------------------------------------
	// Members
	public terrainGeneration m_terrainService;
	public GameObject m_currentLocation;
	public GameObject m_terrainPiece;
	public GameObject m_settlementPiece;
	
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	
	//---------------------------------------------------------------------------------------------
	// Init
	void Start () 
	{
		m_terrainService = GameObject.Find("TerrainService").GetComponent<terrainGeneration>();
		loc_x = (terrainGeneration.terrainHeight/2);
		loc_y = (terrainGeneration.terrainHeight/2);
	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions
	void Update () 
	{
		
		if (m_terrainService.terraincount == m_terrainService.terrainmax)
		{
			Destroy (gameObject);
		}
		if (movecount == movemax)
		{
			int x = Random.Range(0,10);
			
			if (x > 6)
			{
			
			}
			Destroy(gameObject);
		}
		
		loc_x = (int) Mathf.FloorToInt (transform.position.x);
		loc_y = (int) Mathf.FloorToInt (transform.position.z);
		
		move();
		makeFloor();
		
		if (movecount == movemax)
		{
			Destroy(gameObject);
		}
		if (m_terrainService.terraincount == m_terrainService.terrainmax)
		{
			Destroy (gameObject);
		}
		
	}
	
	//---------------------------------------------------------------------------------------------

	void move()
	{
		int choice = Random.Range (1,100);
		//int chance = Random.Range (1,100);	
	
		
		
		
		if (movecount < (movemax-1))
		{
			if (choice < 90)
			{
				transform.Translate (transform.forward * 1, Space.World);
		
				
			}
			if ((choice > 80) && (choice < 87))
			{
				transform.Rotate (0,90,0);
	
				
			}
			if ((choice > 87) && (choice < 95))
			{
				transform.Rotate (0,-90,0);
		
				
			}
			if ((choice > 90) && (choice < 101))
			{
				transform.Rotate (0,180,0);
			
				//Instantiate (gameObject, transform.position, transform.rotation);
				
			}
			if ((choice > 95) && (choice < 101))
			{
				//makeSettlement();
			
			}
			
		}
		
		
		if (loc_x >= (terrainGeneration.terrainWidth-1))
		{
			transform.Rotate (0,180,0);
		}
		if (loc_y >= (terrainGeneration.terrainHeight-1))
		{
			transform.Rotate (0,180,0);
		}
		
		movecount ++;
	}
	
	//---------------------------------------------------------------------------------------------
	void makeSettlement()
	{
		GameObject piece = Instantiate (m_settlementPiece, new Vector3 (loc_x, 0, loc_y), Quaternion.identity) as GameObject;
		piece.transform.parent = GameObject.Find ("SettlementContainer").transform;
		piece.GetComponent<terrainPiece>().settled = true;
		piece.GetComponent<terrainPiece>().location_x = loc_x;
		piece.GetComponent<terrainPiece>().location_y = loc_y;
		piece.GetComponent<terrainPiece>().choosepiece_v2(loc_x, loc_y);
		m_terrainService.m_terrainPieces[loc_x,loc_y] = piece;
		m_terrainService.m_settlementPieces.Add(piece);

	}
	
	//---------------------------------------------------------------------------------------------
	void makeWilderness()
	{
		GameObject piece = Instantiate (m_terrainPiece, new Vector3 (loc_x, 0, loc_y), Quaternion.identity) as GameObject;
		piece.transform.parent = GameObject.Find ("TerrainContainer").transform;
		piece.GetComponent<terrainPiece>().terrainOpen = true;
		piece.GetComponent<terrainPiece>().location_x = loc_x;
		piece.GetComponent<terrainPiece>().location_y = loc_y;
		piece.GetComponent<terrainPiece>().choosepiece_v2(loc_x, loc_y);
		m_terrainService.m_terrainPieces[loc_x,loc_y] = piece;
	
	}
	
	//---------------------------------------------------------------------------------------------
	void makeFloor()
	{
		if (m_terrainService.m_activeTerrain[loc_x,loc_y] == false)
		{
			
			if (movecount == (movemax-1))
			{
				Instantiate (gameObject, transform.position, transform.rotation);
				makeSettlement();
			}
			else
			{
				makeWilderness();
			}
				
			
			m_terrainService.m_activeTerrain[loc_x,loc_y] = true;
		
			m_terrainService.terraincount ++;
		}
		
		
	}

}
